package api

import (
	"github.com/gin-gonic/gin"
	"go-admin-server/app/dao/model"
	"go-admin-server/app/dto/response"
	"go-admin-server/app/service"
	"go-admin-server/common/base"
	"go-admin-server/common/ginx"
	"net/http"
)

type roleApi struct{}

func NewRoleHttp(group *gin.RouterGroup) {
	s := &roleApi{}
	group.GET("/role", s.List)
	group.POST("/role", s.Create)
	group.DELETE("/role", s.Delete)
}

func (s roleApi) List(ctx *gin.Context) {
	page := ginx.QueryInt(ctx, "page", 1)
	size := ginx.QueryInt(ctx, "size", 10)
	list, count := service.RoleService.List(page, size)
	response.SuccessPage(ctx, page, size, int(count), &list)
}

func (s roleApi) Create(ctx *gin.Context) {
	var role model.Role
	ginx.BindJsonAndValid(ctx, &role)
	id, err := service.RoleService.Create(&role)
	if err != nil {
		response.Fail(ctx, 500, err.Error())
	} else {
		response.Success(ctx, id)
	}
}

func (s roleApi) Delete(c *gin.Context) {
	var ids []int
	ginx.BindJsonAndValid(c, &ids)
	if err := service.RoleService.Delete(&ids); err != nil {
		c.JSON(http.StatusOK, base.ServerError())
		return
	}
	c.JSON(http.StatusOK, base.Result{})
}

func (s roleApi) Update(ctx *gin.Context) {

}
